package com.spukmk3me.impl.libgdx;

import com.spukmk3me.sound.Sound;
import com.spukmk3me.sound.SoundInstance;

class LibgdxSoundInstance implements SoundInstance
{
    public LibgdxSoundInstance( Sound sound )
    {
        if ( sound instanceof LibgdxSound )
        {
            this.sound = (LibgdxSound)sound;
            this.music = null;
        }
        else
        {
            this.music = (LibgdxMusic)sound;
            this.sound = null;
        }

        m_state = STATE_READY;
    }
    
    /**
     *   Set the number of loops.
     *   @param numberOfLoops Number of loops, -1 for infinite loops.
     *  If this parameter gets a negative value other than -1, the operation
     *  will be ignored.
     */
    public void setNumberOfLoops( int numberOfLoops )
    {
        if ( numberOfLoops < -1 )
            return;
        
        m_nLoop = numberOfLoops;
    }
    
    /**
     *   Get the number of loops.
     *   @return Number of loops was set. If the number of loops is infinite,
     *  returns -1. Default is 1.
     */
    public int getNumberOfLoops()
    {
        return m_nLoop;
    }
    
    /**
     *   Set the volume for this sound object.
     *   The default number is 1.0.
     */
    public void setVolume( float volume )
    {
        if ( volume < 0 )
            m_volume = 0f;
        else
            m_volume = volume;
        
        if ( sound != null )
            sound.getSound().setVolume( m_id, m_volume );
        else
            music.getMusic().setVolume( m_volume );
    }
    
    /**
     *   Get the current volume of this sound object.
     *   @return Current volume of this sound.
     */
    @Override
    public float getVolume()
    {
        return m_volume;
    }
    
    @Override
    public Sound getOriginalSound()
    {
        if ( sound != null )
            return sound;
        
        return music;
    }

    @Override
    public void play()
    {
        if ( m_state == STATE_READY )
        {
            if ( sound != null )
            {
                if ( m_nLoop == -1 )
                    m_id = sound.getSound().loop( m_volume );
                else
                    m_id = sound.getSound().play( m_volume );
            }
            else
            {
                music.getMusic().setLooping( m_nLoop == -1 );
                music.getMusic().play();
            }
            
            m_state = STATE_PLAYING;
        }
    }

    @Override
    public void stop()
    {
        if ( m_state == STATE_PLAYING )
        {
            if ( sound != null )
                sound.getSound().stop( m_id );
            else
                music.getMusic().stop();
            
            m_state = STATE_STOPPED;
        }
    }
    
    private static final byte STATE_READY   = 0;
    private static final byte STATE_PLAYING = 1;
    private static final byte STATE_STOPPED = 2;
    
    final LibgdxSound sound;
    final LibgdxMusic music;
    
    private long        m_id; // For sound only
    private float       m_volume;
    private int         m_nLoop;
    private byte        m_state;
}
